var ldap = require('ldapjs');
 var obj = [
  {
    dn: 'dc=structure-net,dc=de',
    attributes: {
      objectclass: ['top', 'organization'],
      o: ['dc=structure-net, dc=de']
    }, 
    children:[
    {
    dn: 'ou=example2,dc=structure-net,dc=de',
    attributes: {
      objectclass: ['top','organizationalunit'],
      ou: ['example2']
    },
    children: [
  	{
    	dn: 'ou=example3,ou=example2,dc=structure-net,dc=de',
    	attributes: {
    	  objectclass: ['top','organizationalunit'],
      	  ou: ['example3']
    	}, children:[]
	}
	]
  }]
}
];
var handlers = {};
var server = ldap.createServer();
function walkThrough(obj) {
console.log(' added ' + obj.dn);
var dn = obj.dn;
 	handlers[dn] = (function(req, res, next) {

	  console.log('base object: ' + req.dn.toString());
  	  console.log('scope: ' + req.scope);
  	  console.log('filter: ' + req.filter.toString());
          console.log('searched ' + dn);
	  if(req.scope=="base") {
              	res.send(obj);
	  } else {
             	res.send(obj);
		obj.children.forEach(function(obj) {
			res.send(obj);
		});
	}
        res.end();
        });
	children = obj.children;
	children.forEach(function(obj) {
		walkThrough(obj);
	}); 
}

server.listen(1389, function() {
	walkThrough(obj[0]);
        server.search(obj[0].dn, function(req, res, next) {
	console.log('base object: ' + req.dn.toString());
	var handler=	handlers[req.dn.toString().replace(/\s+/g, '')];
	console.log(handler);
	handler(req,res,next);
});
console.log('LDAP server listening at %s', server.url);
});
